﻿@{
    ViewBag.Title = "服务类别列表";
}

<div class="row">
    <div class="col-xs-12">
        <p>
            <div class="row">
                <div class="col-sm-6">
                    <div class="input-group-btn">
                        状态:
                        <select id="query-status">
                            <option value="1"  selected="selected">正常</option>
                            <option value="2">已禁用</option>
                            <option value="3">已删除</option>
                        </select>
                        <a class="btn btn-sm btn-primary" href="@Url.Content("~/apms/advertisement/create")">
                            <i class="icon-plus bigger-110"></i>创建
                        </a>
                        <button class="btn btn-sm btn-danger" id="btnDelete">
                            <i class="icon-remove bigger-110"></i>删除
                        </button>
                    </div>
                </div>
                <div class="col-sm-6 right">
                </div>
            </div>
        </p>
        <div class="table-responsive">
            <table id="businessTable" class="table table-striped table-bordered table-hover">
                <thead>
                    <tr>
                        <th><input type="checkbox" id="selectAll" /></th>
                        <th>Code</th>
                        <th>名称</th>
                        <th>拼音</th>
                        <th>首字母</th>
                        <th>状态</th>
                        <th>创建时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>
@section inlineScript
{
    <script type="text/javascript">
    var oTable = null;
    $(document).ready(function () {
        $('#com-search-input').bind('keypress', function (event) {
            if (event.keyCode == "13") {
                queryAdvertisementList();
            }
        });
        $('#com-search-icon').on('click', function () {
            queryAdvertisementList();
        });
        $('#query-status').on('change', function () {
            queryAdvertisementList();
        });
        $('#selectAll').on('change', function () {
            $('input[name=selectItem]').each(function (index, element) {
                if (element.checked) {
                    $(element).prop('checked', false);
                } else {
                    $(element).prop('checked', true);
                }
            });
        });
        $('#btnDelete').on('click', function () {
            var idArray = [];
            $('input[name=selectItem]').each(function (index, element) {
                if (element.checked) {
                    idArray.push($(element).attr('data-id'));
                }
            });
            onDeleteMany(idArray);
        });

        queryAdvertisementList();
    });

    function queryAdvertisementList() {
        if (oTable) {
            oTable.fnClearTable();
        }
        oTable = $("#businessTable").dataTable({
            bRetrieve: true,
            "bLengthChange": false, //改变每页显示数据数量
            "iDisplayLength": 20,
            "bFilter": false, //过滤功能
            "bProcessing": false, // 是否显示取数据时的那个等待提示
            "bServerSide": true,//这个用来指明是通过服务端来取数据
            "sAjaxSource": "@Url.Content("~/API/ASMS/Query/ServiceCategoryPagedList")",//这个是请求的地址
            "fnServerData": retrieveData, // 获取数据的处理函数
            "aoColumns": [
                {
                    //"sTitle": "Name",
                    "sWidth": '40px',
                    mData:null,
                    bSortable: false,
                    "mRender": function (data, type, full) {
                        return '<input type="checkbox" name="selectItem" data-id="' + full.id+ '"/>';
                    }
                },
                {
                    mData: "code",
                    bSortable: false
                },
                {
                    mData: "name",
                    bSortable: false
                },
                {
                    mData: "pinyin",
                    bSortable: false
                },
                {
                    mData: "letter",
                    bSortable: false
                },
                {
                    bSortable: false,
                    mData: "status",
                    mRender: function (data, type, full) {
                        if (data == 1) {
                            return "正常";
                        } else if (data == 2) {
                            return "已禁用";
                        } else if (data == 3) {
                            return "已删除";
                        }
                        return 'unknown';
                    }
                },
                {
                    mData: "createdTime",
                    bSortable: false
                },
                {
                    //"sTitle": "Operation",
                    mData: null,
                    "sWidth": '200px',
                    bSortable: false,
                    "mRender": function (data, type, full) {
                        return '<a href="javascript:void(0)" onclick="onDelete(this);" data-id="' + full.id + '">删除</a>';
                    }
                }
            ]
            //,"aoColumnDefs": [{ "bSortable": false, "aTargets": [0, 1, 2, 3] }]
        });
    }

    // 3个参数的名字可以随便命名,但必须是3个参数,少一个都不行
        function retrieveData(sSource, aoData, fnCallback) {
            var start = 0;
            var limit = 0;
            var propertyNames = new Array();
            var sortIndex = 0;
            var sortDirection;
            for (var index = 0; index < aoData.length; index++) {
                if (aoData[index].name == 'iDisplayStart') {
                    start = aoData[index].value;
                } else if (aoData[index].name == 'iDisplayLength') {
                    limit = aoData[index].value;
                } else if (aoData[index].name.indexOf('mDataProp_') == 0) {
                    propertyNames.push(aoData[index].value);
                } else if (aoData[index].name.indexOf('iSortCol_') == 0) {
                    sortIndex = aoData[index].value;
                } else if (aoData[index].name.indexOf('sSortDir_') == 0) {
                    sortDirection = aoData[index].value;
                }
            }
            $.ajax({
                url: sSource,//这个就是请求地址对应sAjaxSource
                data: {
                    "filter": $('#com-search-input').val(),
                    "status": $('#query-status').val(),
                    'start': start,
                    'limit': limit
                },//, sortName: propertyNames[sortIndex], sortDirection: sortDirection },
                type: 'get',
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.isSuccess) {
                        fnCallback({
                            sEcho: aoData[0].value,
                            iTotalRecords: result.data.total,
                            iTotalDisplayRecords: result.data.total,
                            aaData: result.data.items
                        });
                    }
                },
                error: function (msg) {
                }
            });
        }

    function onDelete(el) {
        var id = $(el).attr('data-id');  // save
        if (!id) {
            return;
        }
        if (window.confirm("是否要删除广告?")) {
            $.ajax({
                type: "POST",
                url: "@Url.Content("~/api/apms/advertisement/delete")",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: JSON.stringify({ 'id': id }),
                success: function (data) {
                    if (data.isSuccess) {
                        queryAdvertisementList();
                    } else {
                        alert(data.message);
                    }
                },
                error: function (data) {
                    alert('error');
                }
            });
        }
    };
        function onDeleteMany(idArray) {
            if (idArray == null || idArray.length == 0)
                return;
            if (window.confirm("是否要删除选中的广告?")) {
                $.ajax({
                    type: "POST",
                    url: "@Url.Content("~/api/apms/advertisement/deleteMany")",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: JSON.stringify({ 'idArray': idArray }),
                    success: function (data) {
                        if (data.isSuccess) {
                            queryAdvertisementList();
                        } else {
                            alert(data.message);
                        }
                    },
                    error: function (data) {
                        alert('error');
                    }
                });
            }
        }
    </script>
}
